package com.xiangxiao.rpan.storage.controller;

import com.alibaba.fastjson.JSON;
import com.xiangxiao.rpan.storage.constant.ResponseCode;
import com.xiangxiao.rpan.storage.constant.ResponseMessage;
import com.xiangxiao.rpan.storage.dto.JsonObject;
import com.xiangxiao.rpan.storage.dto.StorageSourceDto;
import com.xiangxiao.rpan.storage.entity.StorageSource;
import com.xiangxiao.rpan.storage.entity.StorageSourceParamDef;
import com.xiangxiao.rpan.storage.entity.SupportStorage;
import com.xiangxiao.rpan.storage.enums.StorageTypeEnum;
import com.xiangxiao.rpan.storage.exception.StorageExcetion;
import com.xiangxiao.rpan.storage.factory.StorageSourceContext;
import com.xiangxiao.rpan.storage.service.StorageSourceService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/8/1 9:57
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class StorageSourceController {
  private static Logger logger = LoggerFactory.getLogger(StorageController.class);
  @Resource
  private StorageSourceService storageSourceService;

  @GetMapping("/storages")
  public JsonObject storageList() {
    try {
      List<StorageSource> list = storageSourceService.findAllOrderByOrderNum();

      return new JsonObject(ResponseMessage.QUERY_SETTING_STORAGE_LIST_SUECCESS, ResponseCode.OK, list);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_SETTING_STORAGE_LIST_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_SETTING_STORAGE_LIST_FAILED, ResponseCode.FAIL);
    }
  }

  @GetMapping("/support-storage")
  public JsonObject supportStorageList() {
    try {
      List<SupportStorage> storages = storageSourceService.getSupportStorages();

      return new JsonObject(ResponseMessage.QUERY_SUPPORT_STORAGE_LIST_SUECCESS, ResponseCode.OK, storages);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_SUPPORT_STORAGE_LIST_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_SUPPORT_STORAGE_LIST_FAILED, ResponseCode.FAIL);
    }
  }

  @GetMapping("/storage-params")
  public JsonObject getFormByStorageType(StorageTypeEnum storageType) {
    try {
      List<StorageSourceParamDef> storageSourceConfigList = StorageSourceContext.getStorageSourceParamListByType(storageType);

      return new JsonObject(ResponseMessage.QUERY_DEFINED_STORAGE_CONFIG_SUECCESS, ResponseCode.OK, storageSourceConfigList);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_DEFINED_STORAGE_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_DEFINED_STORAGE_CONFIG_FAILED, ResponseCode.QUERY_DEFINED_STORAGE_CONFIG_FAILED);
    }
  }

  @GetMapping("/storage/exist/key")
  public JsonObject existKey(String storageKey) {
    try {
      boolean exist = storageSourceService.existByStorageKey(storageKey);
      return new JsonObject(ResponseMessage.INSPECT_STORAGE_KEY_REPEAT_SUECCESS, ResponseCode.OK, exist);
    } catch (Exception e) {
      logger.error(ResponseMessage.INSPECT_STORAGE_KEY_REPEAT_FAILED, e);
      return new JsonObject(ResponseMessage.INSPECT_STORAGE_KEY_REPEAT_FAILED, ResponseCode.INSPECT_STORAGE_KEY_REPEAT_FAILED);
    }
  }

  @PostMapping("/storage")
  public JsonObject saveStorageItem(@RequestBody StorageSourceDto storageSourceDto) {
    try {
      Integer id = storageSourceService.saveStorageSource(storageSourceDto);
      return new JsonObject(ResponseMessage.SAVE_STORAGE_CONFIG_SUECCESS, ResponseCode.OK, id);
    } catch (Exception e) {
      logger.error(ResponseMessage.SAVE_STORAGE_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.SAVE_STORAGE_CONFIG_FAILED, ResponseCode.SAVE_STORAGE_CONFIG_FAILED);
    }
  }

  @GetMapping("/storage/{storageId}")
  public JsonObject queryStorageItem(@PathVariable Integer storageId) {
    try {
      StorageSourceDto storageSourceDto = storageSourceService.findDTOById(storageId);
      return new JsonObject(ResponseMessage.QUERY_STORAGE_CONFIG_SUECCESS, ResponseCode.OK, storageSourceDto);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_STORAGE_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_STORAGE_CONFIG_FAILED, ResponseCode.QUERY_STORAGE_CONFIG_FAILED);
    }
  }

  @PostMapping("/storage/{storageId}/disable")
  public JsonObject disableStorageSource(@PathVariable Integer storageId) {
    try {
      StorageSource storageSource = storageSourceService.findById(storageId);
      storageSource.setEnable(false);
      storageSourceService.updateById(storageSource);
      return new JsonObject(ResponseMessage.DISABLED_STORAGE_CONFIG_SUECCESS, ResponseCode.OK, true);
    } catch (Exception e) {
      logger.error(ResponseMessage.DISABLED_STORAGE_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.DISABLED_STORAGE_CONFIG_FAILED, ResponseCode.DISABLED_STORAGE_CONFIG_FAILED);
    }
  }

  @PostMapping("/storage/{storageId}/compatibility_readme/{status}")
  public JsonObject changeCompatibilityReadme(@PathVariable Integer storageId, @PathVariable Boolean status) {
    try {
      StorageSource storageSource = storageSourceService.findById(storageId);
      storageSource.setCompatibilityReadme(status);
      storageSourceService.updateById(storageSource);
      return new JsonObject(ResponseMessage.MODIFY_README_COMPATIBLE_SUCCESS, ResponseCode.OK, true);
    } catch (Exception e) {
      logger.error(ResponseMessage.MODIFY_README_COMPATIBLE_FAILED, e);
      return new JsonObject(ResponseMessage.MODIFY_README_COMPATIBLE_FAILED, ResponseCode.MODIFY_README_COMPATIBLE_FAILED);
    }
  }

  @DeleteMapping("/storage/{storageId}")
  public JsonObject deleteStorageItem(@PathVariable Integer storageId) {
    try {
      StorageSource source = storageSourceService.deleteStorageById(storageId);
      return new JsonObject(ResponseMessage.DELETE_README_COMPATIBLE_SUCCESS, ResponseCode.OK, source);
    } catch (Exception e) {
      logger.error(ResponseMessage.DELETE_README_COMPATIBLE_FAILED, e);
      return new JsonObject(ResponseMessage.DELETE_README_COMPATIBLE_FAILED, ResponseCode.DELETE_README_COMPATIBLE_FAILED);
    }
  }


}
